package com.freeter.luceneUtil;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.freeter.model.lucene.Gps;
import com.freeter.test.TimeTest;

public class SQLTest {
	private static String driveClassName = "oracle.jdbc.driver.OracleDriver";
	private static String url = "jdbc:oracle:thin:@10.20.20.2:1521:fzbd";

	private static String user = "FZBDDATA";
	private static String password = "FZBDDATA";

  	/*public static void main(String[] args) throws Exception {
		Connection conn = DriverManager.getConnection(url, user, password);
		// 创建SQL执行工具
		QueryRunner qRunner = new QueryRunner();
		Long now1 =TimeTest.nowTime();
		List<Gps> list = qRunner.query(conn,
				"select carid,teamid,carno,callno,orgname,x,y from FZBDDATA.t_yu_jing_stat ",
				new BeanListHandler<Gps>(Gps.class));
		//LuceneUtil.javabean2document(list) ;
		TimeTest.endTime(now1);
  
		 
		IndexWriterConfig config = new IndexWriterConfig(LuceneUtil.fieldAnalyze("carid"));
		IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory()	,config);
		List<Document> dlist  = new ArrayList();
		for(Gps g: list){
			
			Document doc = LuceneUtil.javabean2document(g);
			dlist.add(doc);
		 
		}
		Long now2 =TimeTest.nowTime();
		
		indexWriter.addDocuments(dlist);
		TimeTest.endTime(now2);
 		indexWriter.close();
	} 
	 */
	
	/**
	 * 根据关键字从索引库中搜索符合条件的内容
	 * @throws  Exception 
	 */ 
 	@org.junit.Test
	public void findIndexDB() throws Exception {
		// 准备工作
		String keywords = "*测试*";
		List<Gps> articleList = new ArrayList<Gps>();
		Directory directory = FSDirectory.open(Paths.get("E:/IndexDBDBDB"));
		DirectoryReader ireader = DirectoryReader.open(directory);
		//Analyzer analyzer = new StandardAnalyzer();
		PatternAnalyzer analyzer = new PatternAnalyzer();
		 
		// 创建IndexSearcher字符流对象
		IndexSearcher indexSearcher = new IndexSearcher(ireader);
		// 创建查询解析器对象
		
		 // 参数一：使用分词器的版本，提倡使用该jar包中的最高版本 参数二：争对document对象中的哪个属性进行搜索
		 
	/*	QueryParser queryParser = new  QueryParser("carid", analyzer);

		// 创建对象对象封装查询关键字
		Query query = queryParser.parse(keywords);*/
		
		 Term t=new Term("carno",keywords);
	      Query query=new WildcardQuery(t);
		// 根据关键字，去索引库中的词汇表搜索
		/*
		  参数一：表示封装关键字查询对象，其它QueryParser表示查询解析器
		 * 参数二：MAX_RECORD表示如果根据关键字搜索出来的内容较多，只取前MAX_RECORD个内容
		 * 不足MAX_RECORD个数的话，以实际为准
		 */
		int MAX_RECORD = 300000;
		long time = TimeTest.nowTime();
		TopDocs topDocs = indexSearcher.search(query, MAX_RECORD);
	TimeTest.endTime(time);
		// 迭代词汇表中符合条件的编号
		for (int i = 0; i < topDocs.scoreDocs.length; i++) {
			// 取出封装编号和分数的ScoreDoc对象
			ScoreDoc scoreDoc = topDocs.scoreDocs[i];
			// 取出每一个编号，例如:0,1,2
			int no = scoreDoc.doc;
			// 根据编号去索引库中的原始记录表中查询对应的document对象
			Document document = indexSearcher.doc(no);
			// 获取document对象中的三个属性值
			String carid = document.get("carid");
			String teamid = document.get("teamid");
			String carno = document.get("carno");
			String callno = document.get("callno");
			String orgname = document.get("orgname");
			String x = document.get("x");
			String y = document.get("y");
			 
			Gps gps = new Gps();
			gps.setX(x);
			gps.setCallno(callno);
			gps.setCarid(carid);
			gps.setCarno(carno);
			gps.setOrgname(orgname);
			gps.setTeamid(teamid);
			gps.setY(y);
			// 将article对象加入到list集合中
			articleList.add(gps);
		}
		
		 
		// 迭代结果集
		for (Gps a : articleList) {
			//System.out.println("carid:"+a.getCarid()+"--carno"+a.getCarno()+"--callno"+a.getCallno());
		}
		System.out.println("size:"+ articleList.size());
	}  
	/*@org.junit.Test
	public void getAllDocuments() throws Exception{
		Directory directory = FSDirectory.open(Paths.get("E:/IndexDBDBDB"));
		DirectoryReader ireader = DirectoryReader.open(directory);
		Analyzer analyzer = new StandardAnalyzer();
		 
		// 创建IndexSearcher字符流对象
		IndexSearcher indexSearcher = new IndexSearcher(ireader);
	 
		int count = ireader.maxDoc();//所有文档数
        for (int i = 0; i < count; i++){
            Document doc = indexSearcher.doc(i);
            
            List<IndexableField> listField = doc.getFields();
            
            for ( int j = 0;j < listField.size(); j++){
                IndexableField index = listField.get(j);
                System.out.println(index.name()+":"+index.stringValue());
            }
        }
        ireader.close();
        directory.close();
	}*/
}
